using System;
using System.Linq;
using System.Windows.Input;
using Crm2011.Framework.Core;
using ScriptEase.Shell.Controls;



namespace ScriptEase.Shell.Commands
{
    internal class MergeWebResourceCommand : ICommand
    {
        private readonly ShellViewModel _shellViewModel;
        private readonly WebResourceStatusView _viewModel;



        public MergeWebResourceCommand( ShellViewModel shellViewModel, WebResourceStatusView viewModel)
        {
            _shellViewModel = shellViewModel;
            _viewModel = viewModel;
        }



        public void Execute(object parameter)
        {
            MappingModel[] selectedItems = _viewModel.SelectedItems.Where(
                item => item.SyncStatus == FileSyncStatus.Conflict).ToArray();

            foreach (MappingModel selectedItem in selectedItems)
            {
                string args = Properties.Settings.Default.MergeToolArgs.Replace("%1", selectedItem.FileInfo.FullName);
                args = args.Replace("%2", ConflictResolver.GetMergeFileName(selectedItem.FileInfo.FullName));
                PublicFunc.RunProcess(Properties.Settings.Default.MergeTool, args);
            }
        }




        public bool CanExecute(object parameter)
        {
            return (_viewModel.SelectedItems.Any(
                item => item.SyncStatus == FileSyncStatus.Conflict ||
                item.SyncStatus == FileSyncStatus.PendingUpdate));
        }



        public event EventHandler CanExecuteChanged;
    }
}